package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Override
    public ResultInfo delete(Long id) {
        Integer count1 = dishMapper.countDishByCategoryId(id);
        if (count1 > 0){
            return  ResultInfo.error("当前菜品分类下还有引用，不能删除");
        }
        Integer count2 = setmealMapper.countSetmealByCategoryId(id);
        if (count2 > 0){
            return ResultInfo.error("当前套餐类还有引用，不能删除");
        }
        categoryMapper.delete(id);
        return ResultInfo.success(null);
    }


    //查询口味列表
    @Override
    public List<Category> findList(Integer type) {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType,type);
        List<Category> categories = categoryMapper.selectList(qw);
        return categories;
    }

    @Override
    public void update(Category category) {
       /* //设置更新人信息
        category.setUpdateUser(1L);
        //设置更新时间信息
        category.setUpdateTime(new Date());*/
        //完成更新
        System.out.println(category);
        categoryMapper.updateById(category);
    }

    @Override
    public void add(Category category) {
        //补全id信息
        category.setId(IdUtil.getSnowflake(1,1).nextId());
        /*//补全创建者和创建时间
        category.setCreateUser(1L);
        category.setCreateTime(new Date());
        //补全更新这和更新时间
        category.setUpdateTime(new Date());
        category.setUpdateUser(1L);*/
        //新增
        categoryMapper.insert(category);
    }
}
